package cn.wolfcode.luowowo.article.Service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyPersistenceStatisVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService{
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private IDestinationService destinationService;

    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public void saveOrUpdate(StrategyDetail strategyDetail,String tags) {

        //设置Strategy的目的地id
        Strategy strategy = strategyMapper.selectByPrimaryKey(strategyDetail.getStrategy().getId());
        Destination dest = strategy.getDest();
        strategyDetail.setDest(dest);
        //获取Summary,截取content的前面200字符作为summary
        String content = strategyDetail.getStrategyContent().getContent();
        if (content.length() > 200) {
            strategyDetail.setSummary(content.substring(0,200));
        }else{
            strategyDetail.setSummary(content);
        }
         //设置author为null
        strategyDetail.setAuthor(null);
        //设置是否为目的地是否为海外
        Boolean abord = destinationService.isAbord(dest.getId());
        strategyDetail.setIsabroad(abord);
        if(strategyDetail.getId() == null){
            //设置创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            StrategyContent strategyContent = new StrategyContent();
            strategyContent.setContent(content);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);

        }else{
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            StrategyContent strategyContent = new StrategyContent();
            strategyContent.setContent(content);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.update(strategyContent);

        }
        //处理标签
        //在保存新的标签之前，先将之前的标签清除掉，以免重复存入
        if(strategyDetail.getId() != null){
            strategyTagMapper.deleteRelation(strategyDetail.getId());
        }
        //获取tags的数组

        String[] tagsArr = tags.split(",");
        if(tagsArr != null && tagsArr.length >0){
            for(String tag:tagsArr){
                //先保存tag到tag表
                StrategyTag strategyTag = new StrategyTag();
                strategyTag.setName(tag);
                strategyTag.setState(StrategyTag.STATE_NORMAL);
                strategyTagMapper.insert(strategyTag);
                //维护detail和tag表的第三张表的id 关系
                strategyDetailMapper.insertTagAndDetailRelation(strategyTag.getId(),strategyDetail.getId());
            }
        }
    }

    public void delete(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
        strategyDetailMapper.deleteTagAndDetailRelation(id);
    }

    public List<StrategyDetail> selectAll() {
      return  strategyDetailMapper.selectAll();
    }

    public void changeState(int state, Long id) {
        strategyDetailMapper.changeState(state,id);
    }

    public StrategyDetail selectDetailById(Long id) {

        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    public List<StrategyDetail> list() {

        return strategyDetailMapper.selectAll();
    }

    public StrategyDetail get(Long sid) {
        return strategyDetailMapper.selectByPrimaryKey(sid);
    }

    public List<StrategyDetail> selectDetailTop3ByDestId(Long id) {
        return strategyDetailMapper.selectDetailTop3ByDestId(id);
    }

    public void updateByRedis(StrategyPersistenceStatisVO v) {
        strategyDetailMapper.updateByRedis(v);
    }
}
